package com.yzg.executors;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author misterWei
 * @create 2020年06月13号:03点05分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.ExecutorsStop")
public class ExecutorsStop {

    public static void main(String[] args) {
        //shutDown();
        shutDownNow();
    }
// STOP状态, 将线程全部停止会将队列的任务返回,并打断正在执行的任务
    private static void shutDownNow() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(()->log.info("1"));
        executorService.execute(()->log.info("2"));
        executorService.execute(()->log.info("3"));
        List<Runnable> runnables = executorService.shutdownNow();
        System.out.println(runnables.size());
    }

    //正常关闭,将线程池状态转化为  SHUTDOWN 使用拒绝策率拒绝再次加入的任务,但是不会影响已经提交过的任务
    private static void shutDown() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(()->{
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("111");
        });
        executorService.shutdown();
    }
}
